//	---------------------------------------------------------------------------
//	jWebSocket - Data Packet API
//	Copyright (c) 2010 Alexander Schulze, Innotrade GmbH
//	---------------------------------------------------------------------------
//	This program is free software; you can redistribute it and/or modify it
//	under the terms of the GNU Lesser General Public License as published by the
//	Free Software Foundation; either version 3 of the License, or (at your
//	option) any later version.
//	This program is distributed in the hope that it will be useful, but WITHOUT
//	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//	FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
//	more details.
//	You should have received a copy of the GNU Lesser General Public License along
//	with this program; if not, see <http://www.gnu.org/licenses/lgpl.html>.
//	---------------------------------------------------------------------------
package com.msstudio.websocket.client.api;

import java.io.UnsupportedEncodingException;

/**
 * Specifies the API for low level data packets which are interchanged between
 * client and server. Data packets do not have a special format at this
 * communication level.
 * @author aschulze
 */
public class WebSocketPacket {
    public static final int FRAMETYPE_UTF8 = 0;
    public static final int FRAMETYPE_BINARY = 1;

    private byte[] data = null;
    private int frameType = FRAMETYPE_UTF8;

    /**
     * Instantiates a new data packet and initializes its value to the passed
     * array of bytes.
     * @param aByteArray byte array to be used as value for the data packet.
     */
    public WebSocketPacket(byte[] aByteArray) {
        data = aByteArray;
    }

    /**
     * Instantiates a new data packet and initializes its value to the passed
     * string using the default encoding.
     * @param aString string to be used as value for the data packet.
     */
    public WebSocketPacket(String aString) {
        if (aString != null) {
            try {
                data = null;
                data = aString.getBytes("UTF-8");
            } catch (UnsupportedEncodingException ex) {
            }
        }
    }

    public void setByteArray(byte[] aByteArray) {
        data = null;
        data = aByteArray;
    }

    public void setString(String aString) {
        data = null;
        data = aString.getBytes();
    }

    public void setString(String aString, String aEncoding)
            throws UnsupportedEncodingException {
        data = null;
        data = aString.getBytes(aEncoding);
    }

    public void setUTF8(String aString) {
        try {
            data = null;
            data = aString.getBytes("UTF-8");
        } catch (UnsupportedEncodingException ex) {
            // ignore exception here
        }
    }

    public void setASCII(String aString) {
        try {
            data = null;
            data = aString.getBytes("US-ASCII");
        } catch (UnsupportedEncodingException ex) {
            // ignore exception here
        }
    }

    public byte[] getByteArray() {
        return data;
    }

    public String getString() {
        return new String(data);
    }

    public String getString(String aEncoding)
            throws UnsupportedEncodingException {
        return new String(data, aEncoding);
    }    

    public String getUTF8() {
        try {
            return new String(data, "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            return null;
        }
    }

    public String getASCII() {
        try {
            return new String(data, "US-ASCII");
        } catch (UnsupportedEncodingException ex) {
            return null;
        }
    }

    /**
     * @return the frameType
     */
    public int getFrameType() {
        return frameType;
    }

    /**
     * @param frameType the frameType to set
     */
    public void setFrameType(int aFrameType) {
        this.frameType = aFrameType;
    }
    
    public int getFrameLength() 
    {
        return this.data.length;
    }
}
